﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SqlServerDriver
{
	class BrokerScripting
	{
		public static String MakeBrokerScript(DbView.Command command, String connectionString)
		{
			switch (command.Name)
			{
				case Cmd.BrokerScriptDropMessage: return ScriptDropMessage(command, InitialiseBrokerObjects(connectionString));
				case Cmd.BrokerScriptDropContract: return ScriptDropContract(command, InitialiseBrokerObjects(connectionString));
				case Cmd.BrokerScriptDropService: return ScriptDropService(command, InitialiseBrokerObjects(connectionString));
				case Cmd.BrokerScriptDropQueue: return ScriptDropQueue(command, InitialiseBrokerObjects(connectionString));
				case Cmd.BrokerScriptEndConversation: return ScriptEndConversation(command, InitialiseBrokerObjects(connectionString));
				case Cmd.BrokerScriptToggleActivation: return ScriptToggleActivation(command, InitialiseBrokerObjects(connectionString));
				case Cmd.BrokerScriptToggleStatus: return ScriptToggleStatus(command, InitialiseBrokerObjects(connectionString));
			}

			return String.Format("/*  Script for {0} not ready */", command[DbView.Command.TableParameterName(0)]);
		}

		private static BrokerObjectCollection InitialiseBrokerObjects(String connectionString)
		{
			BrokerObjectCollection bo = new BrokerObjectCollection();
			bo.Populate(connectionString);
			return bo;
		}

		private static string ScriptDropMessage(DbView.Command command, BrokerObjectCollection brokerObjectCollection)
		{
			String messageName = command[DbView.Command.TableParameterName(0)];
			return brokerObjectCollection.ScriptDropMessage(messageName);
		}

		private static string ScriptDropQueue(DbView.Command command, BrokerObjectCollection brokerObjectCollection)
		{
			String queueName = command[CmdParam.QueueName]; // try to find named param. will return "" if param not set
			if (queueName.Length == 0)								// if not found assume its 1st column of report
				queueName = command[DbView.Command.TableParameterName(0)];
			return brokerObjectCollection.ScriptDropQueue(queueName);
		}

		private static string ScriptDropContract(DbView.Command command, BrokerObjectCollection brokerObjectCollection)
		{
			String contractName = command[CmdParam.ContractName];
			return brokerObjectCollection.ScriptDropContract(contractName);
		}

		private static string ScriptDropService(DbView.Command command, BrokerObjectCollection brokerObjectCollection)
		{
			String serviceName = command[CmdParam.ServiceName]; // try to find named param. will return "" if param not set
			if (serviceName.Length == 0)								// if not found assume its 1st column of report
				serviceName = command[DbView.Command.TableParameterName(0)];
			return brokerObjectCollection.ScriptDropService(serviceName);
		}

		private static string ScriptEndConversation(DbView.Command command, BrokerObjectCollection brokerObjectCollection)
		{
			String conversationHandle = command[CmdParam.ConversationHandle];
			return String.Format("end conversation '{0}' with cleanup", conversationHandle);
		}

		private static string ScriptToggleStatus(DbView.Command command, BrokerObjectCollection brokerObjectCollection)
		{
			String queueName = command[CmdParam.QueueName]; // try to find named param. will return "" if param not set
			if (queueName.Length == 0)								// if not found assume its 1st column of report
				queueName = command[DbView.Command.TableParameterName(0)];

			String state = command[CmdParam.StatusFlag];
			state = (state.ToUpper() == "ON") ? "OFF" : "ON";
			return String.Format("Alter QUEUE {0} WITH STATUS = {1}", queueName, state);
		}

		private static string ScriptToggleActivation(DbView.Command command, BrokerObjectCollection brokerObjectCollection)
		{
			String queueName = command[CmdParam.QueueName]; // try to find named param. will return "" if param not set
			if (queueName.Length == 0)								// if not found assume its 1st column of report
				queueName = command[DbView.Command.TableParameterName(0)];

			String procName = command[CmdParam.ProcedureName];
			if (procName.Length == 0)
				return "-- Cannot change activation on a queue without an activation stored procedure";
			String state = command[CmdParam.ActivationFlag];
			state = (state.ToUpper() == "YES") ? "OFF" : "ON";
			return String.Format("Alter QUEUE {0} WITH activation (STATUS = {1})", queueName, state);
		}
	}
}
